package com.dcloud.cfg;


import com.dcloud.manage.service.entity.CollectGlobalCfg;
import com.dcloud.manage.service.ICollectGlobalCfgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author shijf
 * @Description //加载全局配置
 * @Date 19:29 2023/3/6
 **/
@Component
public class GlobalConfigRunner implements ApplicationRunner {

    @Autowired
    ICollectGlobalCfgService iCollectGlobalCfgService; ;

    /**
     * @Author shijf
     * @Description //加载数据库中的全局配置
     * @Date 19:29 2023/3/6
     * @param args
     **/
    @Override
    public void run(ApplicationArguments args) throws Exception {

        List<CollectGlobalCfg> list = iCollectGlobalCfgService.list();
        Map<String, List<CollectGlobalCfg>> collect = list.stream().collect(Collectors.groupingBy(CollectGlobalCfg::getCfgCode));
        collect.forEach((key,value) -> {
            GlobalConfig.addCfg(key,value.get(0).getCfgValue());
        });
    }
}
